package com.mnt.gui.base.launcher;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

import com.mnt.gui.base.controller.BaseController;
import com.mnt.gui.base.logger.TipDialogLogger;
import com.mnt.gui.base.style.WindowStyle;

/**
 * javafx application base class
 * @author mnt.cico
 *
 */
public abstract class BaseLauncher extends Application{
	private static WindowStyle windowStyle = WindowStyle.WINDOW_MNT; // default use mnt border
	
	@Override
	public void start(Stage stage) throws Exception {
		BaseController.setStage(stage);
		TipDialogLogger.init(stage);
		loadSource();
		BaseController root = getRoot();
		PageController.addIndexController(root);		
		windowStyle.buildStageByStyle(stage, root);
		windowStyle.setTitle(getTitle());
		stage.show();
		stage.centerOnScreen();
		stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
			@Override
			public void handle(WindowEvent event) {
				Platform.exit();
				System.exit(1);
				
			}
		});
	}
	
	/**
	 * load source before launch
	 */
	protected void loadSource(){
		
	}
	
	/**
	 * set window style
	 * @param style
	 */
	protected static void setWindowStyle(WindowStyle style) {
		windowStyle = style;
	}
	
	/**
	 * set title
	 * @param title
	 */
	protected String getTitle() {
		return "MNT";
	}
	
	protected abstract BaseController getRoot(); 
	
	static void setRoot(BaseController root) {
		windowStyle.setRoot(root);
	}
}
